<template>
  <div>
    <el-row>
      <el-col>
        <el-form :inline="true" :model="formInline">
          <el-form-item>
            <el-input size="mini" v-model="formInline.instrumentNum" placeholder="请输入设备编号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input size="mini" v-model="formInline.instrumentName" placeholder="请输入设备名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row class="buttonList">
      <el-col>
        <el-button type="primary" size="mini" @click="dialogFormVisible = true">归还</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="4">
        <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
      </el-col>
      <el-col :span="20">
        <el-table
                :data="tableData"
                size="mini"
                border
                style="width: 100%">
          <el-table-column
                  align="center"
                  type="selection"
                  width="55">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="UPDATEUSERNAME"
                  label="操作人">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="INSTRUMENTNAME"
                  label="设备名称">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="INSTRUMENTNUM"
                  label="设备编号">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="CLASSIFICATIONNAME"
                  label="归类属性">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="ENDTIME"
                  label="归还日期">
            <template slot-scope="scope">
              {{new Date(scope.row.ENDTIME).toLocaleString()}}
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                layout="total, sizes, prev, pager, next, jumper"
                :page-size="pagination.pageSize"
                :total="pagination.total"
                :page-sizes="[10, 20, 30, 50, 100]"
        >
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog title="设备归还" center :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" ref="form" label-width="60px">
        <el-row>
          <el-col>
            <el-form-item label="设备ID" autofocus>
              <el-input size="mini" v-model="form.instrumentId" :autofocus="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "returnLog",
    data() {
      return{
        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}],
        dialogImageUrl: '',
        dialogVisible: false,
        dialogFormVisible3: false,
        form: {
          instrumentId: ''
        },
        form3: {},
        dialogFormVisible: false,
        form2:{
          deviceName: [ {name: ''}, {num: 1212}]
        },
        formLabelWidth: '120px',
        dialogFormVisible2: false,
        formInline: {
          departmentId: '',
          testBaseId: '',
          testId: ''
        },
        data: [{
          label: '同济科技职业学院',
          children: [{
            label: '校区1',
            children: [{
              label: '学院',
              children: [{
                label: "实训基地",
                children: [{
                  label: "实训室"
                }]
              }]
            }]
          }]
        }],
        defaultProps: {
          children: 'list',
          label: 'NAME'
        },
        tableData: [],
        pagination: {
          pageSize: 20,
          total: 1,
          pageNo: 1
        },
      }
    },
    created() {
      this.getData(this.pagination.pageSize, this.pagination.pageNo);
      this.getTreeData();
    },
    methods: {
      getData(pageSize, pageNo) {
        this.$post('/insrtumentReceive/findInstrumentReceiveByBorrow', {isEnd: 1, pageSize,pageNo}).then(res => {
          this.tableData = res.data.data.dataList;
        })
      },
      getTreeData() {
        this.$post('/public/findStructureByTestBase').then(res => {
          this.data = res.data.data;
        })
      },
      search() {
        this.formInline.pageSize = this.pagination.pageSize;
        this.formInline.pageNo = 1;
        this.$post('/insrtumentReceive/findInstrumentReceiveByBorrow', this.formInline).then(res => {
          this.tableData = res.data.data.dataList;
        })
      },
      submit() {
        this.$post('/insrtumentReceive/revert', {instrumentId: this.form.instrumentId}).then(res => {
          console.log(res)
          if(res.data.status === 1) {
            this.$message.success('归还成功');
            this.form.instrumentId = '';
          } else {
            this.$message.error('归还失败')
          }
        })
      },
      handleSizeChange(size) {
        this.pagination.pageSize = size;
        this.getData(size, this.pagination.pageNo);
      },
      handleCurrentChange(page) {
        this.pagination.pageNo = page;
        this.getData(this.pagination.pageSize, page);
      },
      handleNodeClick(data, node) {
        console.log(data);
        this.formInline.departmentId = '';
        this.formInline.testBaseId = '';
        this.formInline.testId = '';
        if(node.level === 1) {
          this.formInline.departmentId = data.ID;
        }
        if(node.level === 2) {
          this.formInline.testBaseId = data.ID;
        }
        if(node.level === 3) {
          this.formInline.testId = data.ID;
        }
        this.search();
      }
    }
  }
</script>

<style scoped lang="scss">
  .search{
    margin-bottom: 10px;
  }
  /deep/.el-select{
    width: 100%;
  }
  .addDev {
    /deep/.el-form-item__content {
      display: flex;
    }
  }
  /deep/.el-divider{
    background: #409eff;
  }
  /deep/.el-divider__text{
    font-size: 18px;
    color: #409eff;
  }
  /deep/.el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100%
  }
</style>
